//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Template_FormBase.h"
#include "FormMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "DBGridEh"
#pragma link "DBGridEhGrouping"
#pragma link "FIBDatabase"
#pragma link "FIBDataSet"
#pragma link "GridsEh"
#pragma link "pFIBDatabase"
#pragma link "pFIBDataSet"
#pragma link "PropFilerEh"
#pragma link "PropStorageEh"
#pragma resource "*.dfm"
TTEMPLATE_BaseForm *TEMPLATE_BaseForm;
//---------------------------------------------------------------------------

__fastcall TTEMPLATE_BaseForm::TTEMPLATE_BaseForm(TComponent* Owner) : TForm(Owner)
{
	PropStorageEh->Section = this->Name;
	Constraints->MinWidth = Width;
	Constraints->MinHeight = Height;
}
//---------------------------------------------------------------------------

void __fastcall TTEMPLATE_BaseForm::FormShow(TObject *Sender)
{
	DataSet->Open();
}
//---------------------------------------------------------------------------

void __fastcall TTEMPLATE_BaseForm::FormHide(TObject *Sender)
{
	DataSet->Close();
}
//---------------------------------------------------------------------------

void __fastcall TTEMPLATE_BaseForm::FormCloseQuery(TObject *Sender, bool &CanClose)
{
	if(Transaction->Active && (Transaction->HasUncommitedUpdates || DataSet->State == dsInsert || DataSet->State == dsEdit)) {
		int result = MessageDlg("Save changes ?", mtConfirmation, TMsgDlgButtons()<<mbYes<<mbNo<<mbCancel, NULL, mbYes);
		switch(result) {
			case mrYes:
				if(DataSet->State == dsInsert || DataSet->State == dsEdit) DataSet->Post();
				Transaction->Commit();
				break;
			case mrNo:
				Transaction->Rollback();
				break;
			case mrCancel:
				CanClose = false;
				break;
		}
	}
}
//---------------------------------------------------------------------------

void __fastcall TTEMPLATE_BaseForm::OkButtonClick(TObject *Sender)
{
	if(!Transaction->HasUncommitedUpdates && !DataSet->Modified) {
		if(Transaction->Active) Transaction->Commit();
		Close();
	}
	else if(MessageDlg("Save changes and exit ?", mtConfirmation, TMsgDlgButtons()<<mbYes<<mbNo, NULL, mbNo) == mrYes) {
		if(DataSet->Modified) DataSet->Post();
		if(Transaction->Active) Transaction->Commit();
		Close();
	}
}
//---------------------------------------------------------------------------

void __fastcall TTEMPLATE_BaseForm::CancelButtonClick(TObject *Sender)
{
	if(!Transaction->HasUncommitedUpdates && !DataSet->Modified) {
		if(Transaction->Active) Transaction->Rollback();
		Close();
	}
	else if(MessageDlg("Cancel changes and exit ?", mtConfirmation, TMsgDlgButtons()<<mbYes<<mbNo, NULL, mbNo) == mrYes) {
		if(DataSet->Modified) DataSet->Cancel();
		if(Transaction->Active) Transaction->Rollback();
		Close();
	}
}
//---------------------------------------------------------------------------

void __fastcall TTEMPLATE_BaseForm::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	if(Shift.Contains(ssCtrl)) {
		switch(Key) {
			case VK_RETURN:
				Key = NULL;
				OkButton->Click();
				break;
		}
	}
	if(Shift.Contains(ssShift)) {
		switch(Key) {
			case VK_ESCAPE:
				Key = NULL;
				CancelButton->Click();
				break;
		}
	}
}
//---------------------------------------------------------------------------

